package com.smallA.service.Impl;

import com.smallA.constant.AccessConstant;
import com.smallA.mapper.UserMapper;
import com.smallA.po.Classroom;
import com.smallA.po.Test;
import com.smallA.pojo.Client;
import com.smallA.service.UserService;
import com.smallA.utils.DataUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户登录等操作的实现类
 *
 * @author 君未洋
 */
@Service("UserService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private DataUtils dataUtils;
    private static BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public List<Client> selectAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    public Client selectClientById(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        return userMapper.selectClientById(id);
    }

    @Override
    public Client selectClientLogin(String username, String password) {
        if (username == null || "".equals(username) || "".equals(password) || password == null) {
            return null;
        }
        return userMapper.selectClientLogin(username, password);
    }

    @Override
    public int deleteClient(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return 0;
        }
        return userMapper.deleteClient(id);
    }

    @Override
    public int insertClient(Client client) {
        if (client == null) {
            return 0;
        }
        client.setPassword(passwordEncoder.encode(client.getPassword()));
        //对用户密码加密后再存入数据库
        return userMapper.insertClient(client);
    }

    @Override
    public Client selectUserByName(String username) {
        if (username == null || "".equals(username)) {
            return null;
        }
        return userMapper.selectClientByName(username);
    }

    @Override
    public List<Classroom> selectClassById(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        return userMapper.selectClassById(id);
    }

    @Override
    public List<Client> selectStuByClassId(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        List<Integer> list = userMapper.selectStuByClassId(id);
        List<Client> clientList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            clientList.add(userMapper.selectClientById(list.get(i)));
        }
        return clientList;
    }

    @Override
    public List<Test> selectTestByClassId(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        List<Test> testList = userMapper.selectTestByClassId(id);
        return testList;
    }

    @Override
    public Classroom selectClassByClassId(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        return userMapper.selectClassByClassId(id);
    }

    @Override
    public String judge(Client client) {
        if (client == null){
            return "login";
        }
        if(client.getRole().getRoleName().equals(AccessConstant.STUDENT_ROLE)){
            return "student/main";
        }else{
            return "teacher/main";
        }
    }
}
